package com.adun.scheduler;

import com.adun.scheduler.controller.SchedulerJobController;
import com.adun.scheduler.properties.SchedulerJobXxlJobProperties;
import com.adun.scheduler.xxljob.DynamicXxlJobSpringExecutor;
import com.xxl.job.core.executor.XxlJobExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ZDF
 * @date 2025/8/8
 */
@Configuration
@Slf4j
@AutoConfigureAfter(ConfigurationPropertiesAutoConfiguration.class)
@ConditionalOnClass(XxlJobExecutor.class)
@ConditionalOnProperty(prefix = SchedulerJobXxlJobHandlerAutoConfiguration.PREFIX, name = "enable", havingValue = "true")
@EnableConfigurationProperties(SchedulerJobXxlJobProperties.class)
public class SchedulerJobXxlJobHandlerAutoConfiguration {

    public static final String PREFIX = "dynamic.scheduler.xxl-job";

    @Autowired
    private SchedulerJobXxlJobProperties schedulerJobXxlJobProperties;

    public SchedulerJobXxlJobHandlerAutoConfiguration() {
        printLogBanner();
        log.info("定时任务XxlJob自动配置启动,可通过{}.enable=false禁用整体", PREFIX);
    }

    private void printLogBanner() {
        String banner = "\n" +
                "___  _   _ _  _ ____ _  _ _ ____    _  _ _  _ _        _ ____ ___     ____ ____ _  _ ____ ___  _  _ _    ____ ____ \n" +
                "|  \\  \\_/  |\\ | |__| |\\/| | |    __  \\/   \\/  |    __  | |  | |__] __ [__  |    |__| |___ |  \\ |  | |    |___ |__/ \n" +
                "|__/   |   | \\| |  | |  | | |___    _/\\_ _/\\_ |___    _| |__| |__]    ___] |___ |  | |___ |__/ |__| |___ |___ |  \\ \n" +
                "                                                                                                                   \n";
        log.info(banner);
    }

    @Bean
    public DynamicXxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        DynamicXxlJobSpringExecutor dynamicXxlJobSpringExecutor = new DynamicXxlJobSpringExecutor();
        dynamicXxlJobSpringExecutor.setAdminAddresses(schedulerJobXxlJobProperties.getAdminAddresses());
        dynamicXxlJobSpringExecutor.setAccessToken(schedulerJobXxlJobProperties.getAccessToken());
        dynamicXxlJobSpringExecutor.setTimeout(schedulerJobXxlJobProperties.getTimeout());
        dynamicXxlJobSpringExecutor.setAppname(schedulerJobXxlJobProperties.getAppName());
        dynamicXxlJobSpringExecutor.setAddress(schedulerJobXxlJobProperties.getAddress());
        dynamicXxlJobSpringExecutor.setIp(schedulerJobXxlJobProperties.getIp());
        dynamicXxlJobSpringExecutor.setPort(schedulerJobXxlJobProperties.getPort());
        dynamicXxlJobSpringExecutor.setLogPath(schedulerJobXxlJobProperties.getLogPath());
        dynamicXxlJobSpringExecutor.setLogRetentionDays(schedulerJobXxlJobProperties.getLogRetentionDays());
        return dynamicXxlJobSpringExecutor;
    }

    @Bean
    public SchedulerJobManager schedulerJobManager(){
        return new SchedulerJobManager();
    }

    @Bean
    @ConditionalOnBean(WebMvcAutoConfiguration.class)
    public SchedulerJobController schedulerJobController() {
        return new SchedulerJobController();
    }

}
